/**
 * This class is used to test the category of assessment.
 */
package com.tcc.assessment;

import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

import com.tcc.qa.test.util.AssertCheck;
import com.tcc.qa.test.util.BaseTest;
import com.tcc.qa.test.util.ButtonFinder;
import com.tcc.qa.test.util.LinkFinder;
import com.tcc.qa.test.util.SelectFinder;
import com.tcc.qa.test.util.SwitchTo;


/**
 * @company CampusCruiser
 * @author Emily_Wang
 * @date 2012-6-4
 */
public class QCategories extends BaseTest {

	@Test(description = "Navigate to the QuestionCategories page.", priority = 1)
	public void navigateToQCategory() {
		LinkFinder.getLinkByText(getWebDriver(), "Academics").click();
		LinkFinder.getLinkByText(getWebDriver(),"1-EmilySection-2012 summer-CampusLevel-Course").click();
		LinkFinder.getLinkByText(getWebDriver(), "Assessments Editor").click();
		LinkFinder.getLinkByText(getWebDriver(), "Question Categories").click();
	}
	@Test(description = "Add Category with null name and alert.", priority = 2)
	public void add_nullName() {
    ButtonFinder.getInputButtonByValue(getWebDriver(), "Add").click();
    SwitchTo.switchToAlert(getWebDriver()).accept();
    AssertCheck.assertAlert(getWebDriver(), "You must enter a name.");
    SwitchTo.switchToAlert(getWebDriver()).accept();
	}
	@Test(description = "Add Category with existed name in campus categories and check alert.", priority = 3)
	public void add_existedName() {
    ButtonFinder.getInputButtonByValue(getWebDriver(), "Add").click();
    SwitchTo.switchToAlert(getWebDriver()).sendKeys("Art");
    SwitchTo.switchToAlert(getWebDriver()).accept();
    AssertCheck.assertAlert(getWebDriver(), "The category is already in campus categories, please choose another.");
    SwitchTo.switchToAlert(getWebDriver()).accept();
	}
	@Test(description = "Add a Category and check.", priority = 4)
	public void add_normalName() {
    ButtonFinder.getInputButtonByValue(getWebDriver(), "Add").click();
    SwitchTo.switchToAlert(getWebDriver()).sendKeys("Emily_Wang");
    SwitchTo.switchToAlert(getWebDriver()).accept();
    AssertCheck.assertMsg(getWebDriver(), "Category has been added successfully.");
    AssertCheck.assertSelect(getWebDriver(), "personalCategories", 0, "Emily_Wang");
	}
	@Test(description = "Add a Category already exist and check.", priority = 5)
	public void add_repeatName() {
    ButtonFinder.getInputButtonByValue(getWebDriver(), "Add").click();
    SwitchTo.switchToAlert(getWebDriver()).sendKeys("Emily_Wang");
    SwitchTo.switchToAlert(getWebDriver()).accept();
    AssertCheck.assertAlert(getWebDriver(), "The category is already available, please choose another.");
    SwitchTo.switchToAlert(getWebDriver()).accept();
    AssertCheck.assertSelect(getWebDriver(), "personalCategories", 0, "Emily_Wang");
	}
	@Test(description="Rename category and check", priority=6)
	public void rename_anotherName(){
		ButtonFinder.getInputButtonByValue(getWebDriver(), "Rename").click();
		AssertCheck.assertAlert(getWebDriver(), "Please select a category to rename.");
		SwitchTo.switchToAlert(getWebDriver()).accept();
		SelectFinder.getSelectOption(getWebDriver(), "personalCategories", 0).click();
		ButtonFinder.getInputButtonByValue(getWebDriver(), "Rename").click(); 
		SwitchTo.switchToAlert(getWebDriver()).sendKeys("Admin");
		SwitchTo.switchToAlert(getWebDriver()).accept();
		AssertCheck.assertMsg(getWebDriver(), "Category has been renamed successfully.");
		AssertCheck.assertSelect(getWebDriver(), "personalCategories", 0, "Admin");
	}
	@Test(description="Delete the category.",priority=7)
	public void delete_category(){
		ButtonFinder.getInputButtonByValue(getWebDriver(), "Delete").click();
		AssertCheck.assertAlert(getWebDriver(), "Please select a category to delete.");
		SwitchTo.switchToAlert(getWebDriver()).accept();
		SelectFinder.getSelectOption(getWebDriver(), "personalCategories", 0).click();
		ButtonFinder.getInputButtonByValue(getWebDriver(), "Delete").click();
		AssertCheck.assertMsg(getWebDriver(), "Category has been deleted successfully.");
		SelectFinder.isEmpty(getWebDriver(), "personalCategories");

	}
   @AfterTest
   public void tearDown()
   {
	   getWebDriver().quit();
   }
}
